package edu.fiu.cis.personalize.server.graph;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

import edu.fiu.cis.personalize.server.graph.store.CountingConcMap;
import edu.fiu.cis.personalize.server.graph.store.NodeEdges;

//A Document object has the attribute "link" which keeps the link to the
//document on the web
@SuppressWarnings("serial")
public class Document extends Node implements Serializable {
	private String title;
	private String content;
	private CountingConcMap clickedByUser = new CountingConcMap();
	private CountingConcMap displayedAfterQuery = new CountingConcMap();
	private CountingConcMap clickedAfterQuery = new CountingConcMap();

	public CountingConcMap getClickedByUser() {
		return clickedByUser;
	}

	public CountingConcMap getDisplayedAfterQuery() {
		return displayedAfterQuery;
	}

	public CountingConcMap getClickedAfterQuery() {
		return clickedAfterQuery;
	}

	/**
	 * 
	 * @param URL
	 */
	public Document(int id, String URL) {
		super(id, URL);
		this.title = null;
		this.content = null;
	}

	/**
	 * 
	 * @return The link to the document on the web
	 */
	public String getURL() {
		return getStringKey();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String toString() {
		return getStringKey();
	}

	@Override
	public NodeType getType() {
		return NodeType.DOCUMENT;
	}

	public void clickedBy(User u, Query q) {
		clickedByUser.incrementCount(u.getId());
		clickedAfterQuery.incrementCount(q.id);

	}

	public int displayedAfterQuery(Query q) {
		return displayedAfterQuery.incrementCount(q.getId());

	}

	public double getClickedByUser(User u) {
		return clickedByUser.get(u.getId()).get();
	}

	public double getTotalClicksByUser() {
		int c = 0;
		for (AtomicInteger i : clickedByUser.values()) {
			c += i.get();
		}
		return c;

	}

	public double getNumDocsClickedByUser() {
		return clickedByUser.keySet().size();
	}

	public double getDisplayedAfterQuery(Query q) {
		return displayedAfterQuery.get(q.getId()).get();
	}

	public double getNumClickedAfterQuery(Query q) {
		return clickedAfterQuery.get(q.getId()).get();
	}

	public static Document create(int id, String url, NodeEdges nodeEdges,
			double rank, String title2, String content2, CountingConcMap cu,
			CountingConcMap daq, CountingConcMap caq) {

		Document d = new Document(id, url);
		d.edges = nodeEdges;
		d.rank = rank;
		d.title = title2;
		d.content = content2;
		d.clickedByUser = cu;
		d.displayedAfterQuery = daq;
		d.clickedAfterQuery = caq;
		return d;
	}

}